﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Icst.Leaflike.Data
{
    static class DataUtil
    {
        private static Dictionary<string, string> Empty = new Dictionary<string, string>();

        public static Dictionary<string, string> ParseLinks(string link_names, string link_urls)
        {
            if (string.IsNullOrEmpty(link_names) || string.IsNullOrEmpty(link_urls))
                return Empty;

            Dictionary<string, string> values = new Dictionary<string, string>();

            string[] names = link_names.Trim().Split(' ');
            string[] urls = link_urls.Trim().Split(' ');
            int count = Math.Min(urls.Count(), names.Count());
            for (int i = 0; i < count; ++i)
            {
                values[names[i]] = urls[i];
            }

            return values;
        }
        public static Dictionary<string, List<string>> ParseAllLinks(string link_names, string link_urls)
        {
            if (string.IsNullOrEmpty(link_names) || string.IsNullOrEmpty(link_urls))
                return new Dictionary<string,List<string>>();

            Dictionary<string, List<string>> values = new Dictionary<string, List<string>>();

            string[] names = link_names.Trim().Split(' ');
            string[] urls = link_urls.Trim().Split(' ');
            int count = Math.Min(urls.Count(), names.Count());
            for (int i = 0; i < count; ++i)
            {
                if (!values.ContainsKey(names[i]))
                    values[names[i]] = new List<string>();
                values[names[i]].Add(urls[i]);
            }

            return values;
        }
        public static string Nvl(string value, string ifnullvalue)
        {
            if (string.IsNullOrEmpty(value))
                return ifnullvalue;
            return value;
        }
    }
}
